package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Rust;
import com.example.entity.User;
import com.example.exception.SelfExceptionEnum;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    //自动导入
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param request
     * @param user
     * @return
     */
    @PostMapping("/login")
    Rust<String> login(HttpServletRequest request , @RequestBody User user){

        //md5加密
        //String password = user.getPassword();
        //password = DigestUtils.md5DigestAsHex(password.getBytes());
        //System.out.println(password);

        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        //查询条件，根据用户名查询
        lqw.eq(User::getUsername, user.getUsername());
        //得到查询结果
        User us = userService.getOne(lqw);

        //判断
        if(us == null){
            return Rust.error("用户名不存在");
        }
        if(!us.getPassword().equals(user.getPassword())){
            return Rust.error("密码错误!");
        }
        //登录成功，就将员工id存入session对象
        request.getSession().setAttribute("user" , user.getId());
        return Rust.success("登录成功！");
    }

    /**
     * 查询所有的数据
     * @return
     */
    @GetMapping("/selectAll")
    Rust<List<User>> selectAll(){
        List<User> list = userService.list();
        log.info("查询了全部用户数据，结果为：" + list.toString());
        return Rust.success(list);
    }

    /**
     * 查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/selectUser")
    Rust<User> selectUser(@PathParam("id") Long id){
        User user = userService.getById(id);
        log.info("查询单个用户数据，用户为：" + user.toString());
        return Rust.success(user);
    }

    /**
     * 添加数据
     * @param user
     * @return
     */
    @PostMapping("/save")
    Rust<String> save(@RequestBody User user){
        log.info("添加了一个用户数据，这个用户数据为：" + user.toString());
        if(user.getUsername().trim().length() == 0 || user.getUsername() == null){
            log.info("添加失败，用户名为空");
            return Rust.error(SelfExceptionEnum.SELF_EXCEPTION_NULL);
        }
        if(user.getPassword().trim().length() == 0 || user.getPassword() == null){
            log.info("添加失败，密码为空");
            return Rust.error(SelfExceptionEnum.SELF_EXCEPTION_NULL);
        }
        if(userService.count(user.getUsername()) > 0){
            return Rust.error(SelfExceptionEnum.SELF_EXCEPTION_USER);
        }
        userService.save(user);
        return Rust.success("添加成功");
    }

    /**
     * 删除单个数据
     * @param id
     * @return
     */
    @DeleteMapping("/removeById")
    Rust<String> removeById(@PathParam("id") Long id){
        log.info("想删除一个用户，id值为：" + id);
        boolean flag = userService.removeById(id);
        if(!flag){
            log.info("此id值不存在，无法删除！");
            return Rust.error("此用户不存在，无法删除！");
        }
        return Rust.success("删除成功");
    }

    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    @DeleteMapping("/removeByIds")
    Rust<String> removeByIds(List<Long> ids){
        log.info("批量删除用户数据！id: ");
//        List<Long> longs = new ArrayList<>();
//        for (long id : ids) {
//            longs.add(id);
//        }
        userService.removeByIds(ids);
        return Rust.success("批量删除成功！");
    }

    /**
     * 修改数据
     * @param user
     * @return
     */
    @PutMapping("/updateUser")
    Rust<String> updateUser(@RequestBody User user){
        log.info("用户数据有修改，修改用户为：" + user.toString());
        if(user.getUsername().trim().length() == 0 || user.getUsername() == null){
            log.info("修改失败，用户名为空");
            return Rust.error(SelfExceptionEnum.SELF_EXCEPTION_NULL);
        }
        if(user.getPassword().trim().length() == 0 || user.getPassword() == null){
            log.info("修改失败，密码为空");
            return Rust.error(SelfExceptionEnum.SELF_EXCEPTION_NULL);
        }
        if(userService.count(user.getUsername()) > 0){
            return Rust.error(SelfExceptionEnum.SELF_EXCEPTION_USER);
        }

        userService.updateById(user);

        return Rust.success("修改成功！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPage")
    Rust<IPage> selectPage(int page , int pageSize){
        //创建IPage分页对象
        IPage<User> userPage = new Page<>(page , pageSize);
        //执行查旬
        userService.page(userPage , null);
        //得到数据
        log.info("查询到的数据：" + userPage.getRecords());
        log.info("总共多少条数据：" + userPage.getTotal());
        log.info("查询当前页数：" + userPage.getCurrent());
        log.info("查询的每页数：" + userPage.getSize());
        log.info("一共多少页: " + userPage.getPages());
        return Rust.success(userPage);
    }

}
